// components/shop-cate/index.js
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            default: false
        },
        list: {
            type: Array,
            default: []
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        newList: [],
    },
    created: function(){ },
    observers: {
        'show': function(newVal){
            this.setData({
                newList: this.data.list
            })
        },
        
    },
    /**
     * 组件的方法列表
     */
    methods: {
        //关闭
        close: function() {
            this.triggerEvent('confirm');  
        },
        //设置补齐
        onChange: function(e) {
            let {newList} = this.data;
            let index = e.currentTarget.dataset.index;
            let v = e.detail;
            newList[index].is_all = v?1:2;
            this.setData({
                newList: newList
            })
        },
        //设置价格和库存及成本
        setValue: function(e) {
            let {newList} = this.data;
            let name = e.currentTarget.dataset.name;
            let index = e.currentTarget.dataset.index;
            let v = e.detail;
            newList[index][name] = v;
            this.setData({
                newList: newList
            })
        },
        //设置value值
        setStepValue: function(e){
            let {newList} = this.data;
            let name = e.currentTarget.dataset.name;
            let index = e.currentTarget.dataset.index;
            let v = e.detail;
            console.log(name,index,v)
            newList[index][name] = v;
            this.setData({
                newList: newList
            })
        },
        //保存
        save: function() {
            let {newList} = this.data;
            let error = '';
            newList.forEach(item=>{
                if(item.price<=0){
                    error = '请设置售卖价'
                }
            })
            if(error){
                wx.showToast({
                    title: error,
                    icon: 'none'
                })
                return;
            }else{
                this.triggerEvent('confirm',{change: true, list:newList});  
            }
        }
    }
})